<script setup>
import { ref } from 'vue'
import { baseURL } from '@/api/request'
import { getSongList, getSongDetail, deleteSong } from '@/api/song'
import SongDetail from './subs/SongDetail.vue'
import EditSong from './subs/EditSong.vue'
import AddListSong from './subs/AddListSong.vue'
import { useSongStore } from '@/stores/song'


const songs = ref([])
const queryParams = ref({
  pageNo: 1,
  size: 5,
  search: ''
})
const total = ref(0)
const songList = async () => {
  const { data } = await getSongList(queryParams.value)
  songs.value = data.records
  total.value = data.total
}
songList()
// 分页
const change = (pageNo, size) => {
  console.log(pageNo, size);
  queryParams.value.pageNo = pageNo
  queryParams.value.size = size
  songList()
}
// 搜索
const submitForm = () => {
  songList()
}

// 删除
const deleteSongMessageBox = async row => { 
  ElMessageBox.confirm('确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => { 
    // 调用删除接口
    deleteSong(row.id).then(res => {
      ElMessage.success(res.msg)
      songList()
    }).catch(err => {
      console.error(err)
      // 已被拦截器弹出提示
      // ElMessage.error('删除失败')
    })
  })
}

const songDetailRef = ref(null)
// 显示抽屉
const showDetail = (row) => {
  // 执行子组件方法
  songDetailRef.value.openDrawer(row)
}

const editSongRef = ref(null)

const addSongFun = () => {
  editSongRef.value.openDrawer()
}

const editSongFun = (row) => {
  editSongRef.value.openDrawer(row.id)
}

// 添加歌单
const addListSongRef = ref(null)
const addListSongFun = (row) => {
  addListSongRef.value.openDrawer(row.id)
}


// 全局播放音乐
const songStore = useSongStore()

// 播放歌曲（调用 Pinia 的 action）
const playSong = (songId) => {
  songStore.addSongToPlaylist(songId)
}

// 歌曲列表
// const songsList = ref([
// ]);

// const playingSong = async row => {
//   const { data } = await getSongDetail(row.id)
//   data.url = baseURL + '/r' + data.url
//   data.pic = baseURL + '/r' + data.pic
//   console.log(data);
//   songsList.value.unshift(data)
// }
</script>

<template>
  <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="queryParams" label-width="auto" class="demo-form-inline">
          <el-form-item label="歌名">
            <el-input placeholder="请输入歌曲信息" clearable v-model="queryParams.search" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">搜</el-button>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="addSongFun">添加</el-button>
      </div>
    </template>
    <el-table :data="songs" style="width: 100%">
      <el-table-column prop="id" label="编号" width="60">
        <template #default="{ row }">
          <el-link type="primary" @click="showDetail(row)">{{ row.id }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="歌曲图片" width="90">
        <template #default="{ row }">
          <el-image :src="baseURL + '/r' + row.pic" fit="cover" />
        </template>
      </el-table-column>
      <el-table-column label="歌名" width="360">
        <template #default="{ row }">
          <el-link type="primary" @click="playSong(row.id)">{{ row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="introduction" label="简介"/>
      <el-table-column label="操作"  width="280">
        <template #default="{ row }">
          <el-button type="primary" @click="editSongFun(row)">编辑</el-button>
          <el-button type="success" @click="addListSongFun(row)">添加歌单</el-button>
          <el-button type="danger" @click="deleteSongMessageBox(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="example-pagination-block">
      <el-pagination background layout="prev, pager, next" @change="change" :total="total"
        :page-size="queryParams.size" />
    </div>
  </el-card>
  <!-- 音乐播放器组件 -->
  <!-- <MusicPlayer :songs="songsList" :singers="singersMap" :autoPlay="true" /> -->
  <!-- 抽屉详情 -->
  <song-detail ref="songDetailRef" />
  <!-- 添加/编辑歌曲 -->
  <edit-song ref="editSongRef" @song-list="songList" />
  <!-- 添加歌单 -->
  <add-list-song ref="addListSongRef" />
</template>

<style scoped>
.el-card {
  min-height: 100%;
  width: 100vw;
}
.card-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>